package com.example.chcrm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.chcrm.entity.common.ResponseMsg;
import com.example.chcrm.entity.common.ServletUtils;
import com.example.chcrm.entity.employee.Department;
import com.example.chcrm.entity.employee.EmployeeVo;
import com.example.chcrm.entity.employee.EssEmployee;
import com.example.chcrm.entity.vehicle.TransportPlanCv;
import com.example.chcrm.service.TransportPlanCvService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author 邹
 * @Date 2025/4/24 11:10
 * @Version 1.0
 */
@RestController
@RequestMapping("/transportPlan")
@CrossOrigin // 允许跨域访问
public class TransportPlanCvController {
    @Autowired
    private TransportPlanCvService transportPlanCvService;
    /**
     * 部门查询
     */
    @PostMapping("/pageList")
    public ResponseMsg<IPage<TransportPlanCv>> pageList(@RequestBody TransportPlanCv department) {

        return  transportPlanCvService.pageList(department);
    }

    @PostMapping("/add")
    public ResponseMsg<Object> add(@RequestBody TransportPlanCv department) {
        HttpServletRequest request = ServletUtils.getRequest();
        EmployeeVo empVo = request != null ? (EmployeeVo) request.getAttribute("empVo") : null;
        return  transportPlanCvService.add(department,empVo);
    }

    @GetMapping("/detail/{id}")
    public ResponseMsg<Object> detail(@PathVariable String id) {
        HttpServletRequest request = ServletUtils.getRequest();
        EmployeeVo empVo = request != null ? (EmployeeVo) request.getAttribute("empVo") : null;
        return transportPlanCvService.detail(id);
    }

    @PostMapping("/update")
    public ResponseMsg<Object> update(@RequestBody TransportPlanCv essEmployee) {
        ResponseMsg<Object> msg = new ResponseMsg<>();
        if (!StringUtils.isNotBlank(essEmployee.getId())) {
            msg.fail("修改id不能为空");
            return msg;
        }
        HttpServletRequest request = ServletUtils.getRequest();
        EmployeeVo empVo = request != null ? (EmployeeVo) request.getAttribute("empVo") : null;
        return  transportPlanCvService.update(essEmployee,empVo);
    }

    /**
     * 删除
     */
    @GetMapping("/delete/{id}")
    public ResponseMsg<Object> delete(@PathVariable String id) {
        ResponseMsg<Object> msg = new ResponseMsg<>();
        if (id == null || id.equals("")) {
            msg.fail("删除失败ID不能为空");
            return msg;
        }
        return transportPlanCvService.delete(id);
    }
}
